"""
配置管理数据模型
定义配置系统使用的数据结构和枚举类型
"""

from datetime import datetime
from enum import Enum
from typing import Optional, List, Dict, Any
from pydantic import BaseModel, Field


class DataType(str, Enum):
    """配置数据类型枚举"""
    STRING = "string"
    INTEGER = "integer"
    FLOAT = "float"
    BOOLEAN = "boolean"
    JSON = "json"
    PATH = "path"
    SECRET = "secret"


class ConfigChangeType(str, Enum):
    """配置变更类型枚举"""
    CREATE = "create"
    UPDATE = "update"
    DELETE = "delete"
    RELOAD = "reload"
    RESTART = "restart"


# 纯Pydantic模型，用于API交互和数据验证
class ConfigGroup(BaseModel):
    """配置分组模型"""
    id: int
    name: str = Field(..., max_length=50, description="分组标识名")
    display_name: str = Field(..., max_length=100, description="分组显示名称")
    description: Optional[str] = Field(None, description="分组描述")
    icon: Optional[str] = Field(None, max_length=50, description="图标类名")
    sort_order: int = Field(0, description="排序顺序")
    is_active: bool = Field(True, description="是否启用")
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class ConfigItem(BaseModel):
    """配置项模型"""
    id: int
    group_id: int = Field(..., description="所属分组ID")
    category: str = Field(..., max_length=50, description="配置分类")
    key_name: str = Field(..., max_length=100, description="配置键名")
    display_name: str = Field(..., max_length=100, description="配置显示名称")
    description: Optional[str] = Field(None, description="配置描述")
    data_type: DataType = Field(..., description="数据类型")
    default_value: Optional[str] = Field(None, description="默认值")
    current_value: Optional[str] = Field(None, description="当前值")
    value_range: Optional[str] = Field(None, description="取值范围JSON")
    validation_rule: Optional[str] = Field(None, description="验证规则")
    is_sensitive: bool = Field(False, description="是否敏感信息")
    is_reload_required: bool = Field(True, description="是否需要重启生效")
    is_readonly: bool = Field(False, description="是否只读")
    placeholder: Optional[str] = Field(None, max_length=200, description="输入提示")
    sort_order: int = Field(0, description="排序顺序")
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class ConfigChange(BaseModel):
    """配置变更记录模型"""
    id: int
    config_key: str = Field(..., description="配置键名")
    config_id: int = Field(..., description="配置项ID")
    old_value: Optional[str] = Field(None, description="旧值")
    new_value: Optional[str] = Field(None, description="新值")
    change_type: ConfigChangeType = Field(..., description="变更类型")
    changed_by: str = Field(..., description="操作用户")
    change_reason: Optional[str] = Field(None, max_length=200, description="变更原因")
    ip_address: Optional[str] = Field(None, max_length=45, description="操作IP地址")
    user_agent: Optional[str] = Field(None, description="用户代理")
    created_at: datetime

    class Config:
        from_attributes = True


class ConfigBackup(BaseModel):
    """配置备份模型"""
    id: int
    backup_name: str = Field(..., max_length=100, description="备份名称")
    backup_description: Optional[str] = Field(None, description="备份描述")
    backup_data: Dict[str, Any] = Field(..., description="备份数据")
    backup_version: Optional[str] = Field(None, description="备份版本")
    is_auto_backup: bool = Field(False, description="是否自动备份")
    created_by: str = Field(..., description="创建用户")
    created_at: datetime
    file_size: Optional[int] = Field(None, description="备份文件大小")
    checksum: Optional[str] = Field(None, max_length=64, description="数据校验和")

    class Config:
        from_attributes = True


class ConfigDependency(BaseModel):
    """配置依赖关系模型"""
    id: int
    config_id: int = Field(..., description="配置项ID")
    depends_on_config_id: int = Field(..., description="依赖的配置项ID")
    dependency_type: str = Field(..., description="依赖类型")
    dependency_condition: Optional[str] = Field(None, description="依赖条件")
    created_at: datetime

    class Config:
        from_attributes = True


class ValidationResult(BaseModel):
    """配置验证结果模型"""
    is_valid: bool = Field(..., description="是否验证通过")
    error_message: Optional[str] = Field(None, description="错误信息")
    normalized_value: Optional[str] = Field(None, description="标准化值")

    class Config:
        from_attributes = True


# API请求/响应模型 (这些不是数据库模型，所以不继承Model)
class ConfigGroupCreate(BaseModel):
    """创建配置分组请求"""
    name: str = Field(..., max_length=50, description="分组标识名")
    display_name: str = Field(..., max_length=100, description="分组显示名称")
    description: Optional[str] = Field(None, description="分组描述")
    icon: Optional[str] = Field(None, max_length=50, description="图标类名")
    sort_order: int = Field(0, description="排序顺序")


class ConfigGroupUpdate(BaseModel):
    """更新配置分组请求"""
    display_name: Optional[str] = Field(None, max_length=100, description="分组显示名称")
    description: Optional[str] = Field(None, description="分组描述")
    icon: Optional[str] = Field(None, max_length=50, description="图标类名")
    sort_order: Optional[int] = Field(None, description="排序顺序")
    is_active: Optional[bool] = Field(None, description="是否启用")


class ConfigItemCreate(BaseModel):
    """创建配置项请求"""
    group_id: int = Field(..., description="所属分组ID")
    category: str = Field(..., max_length=50, description="配置分类")
    key_name: str = Field(..., max_length=100, description="配置键名")
    display_name: str = Field(..., max_length=100, description="配置显示名称")
    description: Optional[str] = Field(None, description="配置描述")
    data_type: DataType = Field(..., description="数据类型")
    default_value: Optional[str] = Field(None, description="默认值")
    value_range: Optional[str] = Field(None, description="取值范围JSON")
    validation_rule: Optional[str] = Field(None, description="验证规则")
    is_sensitive: bool = Field(False, description="是否敏感信息")
    is_reload_required: bool = Field(True, description="是否需要重启生效")
    is_readonly: bool = Field(False, description="是否只读")
    placeholder: Optional[str] = Field(None, max_length=200, description="输入提示")
    sort_order: int = Field(0, description="排序顺序")


class ConfigItemUpdate(BaseModel):
    """更新配置项请求"""
    value: str = Field(..., description="新配置值")
    change_reason: Optional[str] = Field(None, max_length=200, description="变更原因")


class ConfigBatchUpdate(BaseModel):
    """批量更新配置项请求"""
    updates: List[Dict[str, Any]] = Field(..., description="更新列表")
    change_reason: Optional[str] = Field(None, max_length=200, description="变更原因")


class ConfigItemBatchUpdate(BaseModel):
    """批量更新配置项请求"""
    updates: List[Dict[str, Any]] = Field(..., description="更新列表")
    change_reason: Optional[str] = Field(None, max_length=200, description="变更原因")


class ConfigUpdate(BaseModel):
    """更新配置项请求"""
    display_name: Optional[str] = Field(None, max_length=100, description="配置显示名称")
    description: Optional[str] = Field(None, description="配置描述")
    current_value: Optional[str] = Field(None, description="当前值")
    value_range: Optional[str] = Field(None, description="取值范围JSON")
    validation_rule: Optional[str] = Field(None, description="验证规则")
    is_sensitive: Optional[bool] = Field(None, description="是否敏感信息")
    is_reload_required: Optional[bool] = Field(None, description="是否需要重启生效")
    is_readonly: Optional[bool] = Field(None, description="是否只读")
    placeholder: Optional[str] = Field(None, max_length=200, description="输入提示")
    sort_order: Optional[int] = Field(None, description="排序顺序")


class ConfigValidationRequest(BaseModel):
    """配置验证请求"""
    key: str = Field(..., description="配置键名")
    value: str = Field(..., description="配置值")


class ConfigBackupCreate(BaseModel):
    """创建配置备份请求"""
    backup_name: str = Field(..., max_length=100, description="备份名称")
    backup_description: Optional[str] = Field(None, description="备份描述")


# API响应模型
class ConfigGroupResponse(BaseModel):
    """配置分组响应"""
    success: bool = Field(True, description="操作是否成功")
    data: Optional[ConfigGroup] = Field(None, description="配置分组数据")
    message: Optional[str] = Field(None, description="响应消息")


class ConfigGroupsResponse(BaseModel):
    """配置分组列表响应"""
    success: bool = Field(True, description="操作是否成功")
    data: List[ConfigGroup] = Field(default_factory=list, description="配置分组列表")
    total: int = Field(0, description="总数量")
    message: Optional[str] = Field(None, description="响应消息")


class ConfigItemResponse(BaseModel):
    """配置项响应"""
    success: bool = Field(True, description="操作是否成功")
    data: Optional[ConfigItem] = Field(None, description="配置项数据")
    message: Optional[str] = Field(None, description="响应消息")


class ConfigItemsResponse(BaseModel):
    """配置项列表响应"""
    success: bool = Field(True, description="操作是否成功")
    data: List[ConfigItem] = Field(default_factory=list, description="配置项列表")
    total: int = Field(0, description="总数量")
    message: Optional[str] = Field(None, description="响应消息")


class ConfigValidationResponse(BaseModel):
    """配置验证响应"""
    success: bool = Field(True, description="操作是否成功")
    data: Optional[ValidationResult] = Field(None, description="验证结果")
    message: Optional[str] = Field(None, description="响应消息")


class ConfigHistoryResponse(BaseModel):
    """配置历史响应"""
    success: bool = Field(True, description="操作是否成功")
    data: List[ConfigChange] = Field(default_factory=list, description="历史记录列表")
    total: int = Field(0, description="总数量")
    message: Optional[str] = Field(None, description="响应消息")


class ConfigBackupResponse(BaseModel):
    """配置备份响应"""
    success: bool = Field(True, description="操作是否成功")
    data: Optional[ConfigBackup] = Field(None, description="备份数据")
    message: Optional[str] = Field(None, description="响应消息")


class ConfigBackupsResponse(BaseModel):
    """配置备份列表响应"""
    success: bool = Field(True, description="操作是否成功")
    data: List[ConfigBackup] = Field(default_factory=list, description="备份列表")
    total: int = Field(0, description="总数量")
    message: Optional[str] = Field(None, description="响应消息")


class ConfigStatusResponse(BaseModel):
    """配置状态响应"""
    success: bool = Field(True, description="操作是否成功")
    data: Dict[str, Any] = Field(default_factory=dict, description="状态数据")
    message: Optional[str] = Field(None, description="响应消息")


class ConfigBatchUpdateResponse(BaseModel):
    """批量更新配置响应"""
    success: bool = Field(True, description="操作是否成功")
    data: Dict[str, bool] = Field(default_factory=dict, description="更新结果")
    message: Optional[str] = Field(None, description="响应消息")


# 通用响应模型
class ApiResponse(BaseModel):
    """API通用响应"""
    success: bool = Field(True, description="操作是否成功")
    message: Optional[str] = Field(None, description="响应消息")
    data: Optional[Any] = Field(None, description="响应数据")
    code: int = Field(200, description="响应代码")


class PaginatedResponse(BaseModel):
    """分页响应"""
    success: bool = Field(True, description="操作是否成功")
    data: List[Any] = Field(default_factory=list, description="数据列表")
    total: int = Field(0, description="总数量")
    page: int = Field(1, description="当前页码")
    page_size: int = Field(20, description="每页数量")
    message: Optional[str] = Field(None, description="响应消息")